@extends('admin.layouts.master')
@section('content')
    <link rel="stylesheet"
          href="{{asset('static/bootstrap/css/bootstrap.min.css')}}">
    <table class="mdui-table mdui-table-hoverable mdui-typo">
        <thead>
        <tr>
            <form action="{{url('admin/weibo/page')}}" method="post">
                {{csrf_field()}}
                <td colspan=2>
                    <select class="form-control" name="is_gai" id="">
                        <option value="1">开启</option>
                        <option value="0">禁用</option>
                    </select>
                </td>
                <td colspan=4>
                    <div class="input-group">
                        <input name="contents" type="text" class="form-control" placeholder="请输入微博内容搜索">
                        <span style="position: absolute;top: 0;right: -50px;z-index: 100;">
                        <button style="height: 34px;width: auto;border-radius: 0 3px 3px 0;border: 1px solid #ccc;"
                                type="submit"  class="input-group-addon btn btn-primary">搜索</button>
                    </span>
                    </div>
                </td>
            </form>
        </tr>
        <tr>
            <th>ID</th>
            <th>用户昵称</th>
            <th>微博内容摘要</th>
            <th>发表时间</th>
            <th>前台可见</th>
            <th>操作</th>
        </tr>
        </thead>

        <tbody>
        @foreach($weibos as $weibo)
        <tr>
            <td>{{$weibo->id}}</td>
            <td>{{$weibo->nickname}}</td>
            <td>{{$weibo->content}}</td>
            <td>{{$weibo->created_at}}</td>
            <td>
                <label class="mdui-switch">
                    <input type="checkbox" is_show="{{$weibo->is_show}}"
                           class="change" {{$weibo->is_show==1?'checked':''}}/>
                    <i class="mdui-switch-icon"></i>
                </label>
            </td>

            <td>
                <a href="/admin/weibo/{{$weibo['id']}}"
                   class="mdui-btn mdui-btn-dense mdui-color-blue mdui-ripple">详情</a>
            </td>
        </tr>
        @endforeach
        </tbody>
    </table>
    <div class="pull-right">
        {{$weibos->render()}}
    </div>
    <!-- jQuery 文件 -->
    <script src="{{asset('static/jquery/jquery.min.js')}}"></script>
    <!-- Bootstrap JavaScript 文件 -->
    <script src="{{asset('static/bootstrap/js/bootstrap.min.js')}}"></script>
@endsection

@section('js')
    <script>
        $(".change").click(function () {
            //获取每次选择得到的id
            var weibo_id = $(this).parent().parent().parent().find('td').first().text();
//            console.log(weibo_id);
            //增加一个标识
            var is_show = $(this).attr('is_show');
            //指定选定的ID
            var mythis = $(this);
            $.ajax({
                url: '/admin/weibo/'+ weibo_id +'/update',
                dataType: 'json',
                type:'get',
                data: {'is_show': is_show},
                success: function (data) {
                    if (data.message === 1) {
                        mythis.attr('checked', '');
                        mythis.attr('is_show', '1');
                    } else {
                        mythis.removeAttr('checked');
                        mythis.attr('is_show', '0');
                    }
                }
            });
        });
    </script>
@endsection
